package com.szyportal.web.front.controller;

import com.szyportal.common.core.controller.BaseController;
import com.szyportal.custom.domain.*;
import com.szyportal.custom.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private IWCarouselService wCarouselService;
    @Autowired
    private IWNoticeService wNoticeService;
    @Autowired
    private IWServicesService wServicesService;
    @Autowired
    private IWAboutService wAboutService;
    @Autowired
    private IWCaseService wCaseService;
    @Autowired
    private IWNewsService wNewsService;
    @Autowired
    private IWLinkService wLinkService;
    @Autowired
    private IWConfigService wConfigService;
    @Autowired
    private IWBusinessService wBusinessService;
    @Autowired
    private IWBrandService wBrandService;
    @Autowired
    private IWDevelopService wDevelopService;
    @Autowired
    private IWHonorCharmService wHonorCharmService;

    @GetMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        // 轮播图
        WCarousel wCarousel = new WCarousel();
        wCarousel.setWType(0);
        List<WCarousel> wCarousels = wCarouselService.selectWCarouselList(wCarousel);
        model.put("wCarousels", wCarousels);
        // 广播
        List<WNotice> wNotices = wNoticeService.selectWNoticeList(null);
        model.put("notices", wNotices);
        String allNoticeContent = wNotices.stream()
                .map(WNotice::getContent)
                .collect(Collectors.joining("  "));
        model.put("allNoticeContent", allNoticeContent);
        // 服务项目
        List<WServices> services = wServicesService.selectWServicesList(null);
        model.put("services", services);
        // 关于我们
        WAbout wAbout = wAboutService.selectWAboutById(1L);
        model.put("wAbout", wAbout);
        // 案例展示
        List<WCase> wCases = wCaseService.selectTopWCaseList();
        model.put("caseList", wCases);
        // 新闻中心
        List<WNews> wNews = wNewsService.selectTopWNewsList();
        model.put("newsList", wNews);
        // 获取友情链接列表
        List<WLink> wLinks = wLinkService.selectWLinkList(null);
        model.put("linkList", wLinks);
        // 业务范围
        List<WBusiness> wBusinesses = wBusinessService.selectWBusinessList(null);
        model.put("wBusiness",wBusinesses);
//        WConfig wConfig = wConfigService.selectWConfigById(1l);
//        model.put("wConfig",wConfig);
        // 品牌文化
        List<WBrand> wBrands = wBrandService.selectWBrandList(null);
        model.addAttribute("wBrands", wBrands);
        // 发展历程
        List<WDevelop> WDevelops = wDevelopService.selectWDevelopList(null);
        model.addAttribute("wDevelops", WDevelops);
        // 企业荣誉与风采
        List<WHonorCharm> WHonorCharms = wHonorCharmService.selectWHonorCharmList(null);
        List<WHonorCharm> honorList = new ArrayList<>();
        List<WHonorCharm> charmList = new ArrayList<>();

        WConfig wConfig = wConfigService.selectWConfigById(1l);
        model.put("wConfig",wConfig);
        WHonorCharms.forEach(item-> {
            if(item.getwType()==0){
                honorList.add(item);
            }else {
                charmList.add(item);
            }
        });
        model.addAttribute("honorList", honorList);
        model.addAttribute("charmList", charmList);
        return "front/index";
    }

    // 查看详情
    @GetMapping("/business/details")
    public String details(HttpServletRequest request, @RequestParam("businessId") String businessId, HttpServletResponse response, ModelMap model) {
        WBusiness wBusiness = wBusinessService.selectWBusinessById(Long.parseLong(businessId));
        WConfig wConfig = wConfigService.selectWConfigById(1l);
        // 业务范围
        List<WBusiness> wBusinesses = wBusinessService.selectWBusinessList(null);
        model.put("wBusinessAll",wBusinesses);
        model.put("wConfig",wConfig);
        model.put("wBusiness", wBusiness);
        return "front/business_details";
    }
}
